import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import '../../http/index.dart';
import '../../utils/storage.dart';
import '../../provider/index.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool loginType = true;
  int userType;
  setLoginType(bool show) {
    setState(() {
      loginType = show;
    });
  }

  setUserType(int type) {
    userType = type;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: loginType
          ? OptionIdentity(setLoginType, setUserType)
          : Login(setLoginType, userType),
    );
  }
}

class OptionIdentity extends StatefulWidget {
  var setLoginType;
  var setUserType;
  OptionIdentity(this.setLoginType, this.setUserType);

  @override
  _OptionIdentityState createState() =>
      _OptionIdentityState(setLoginType, setUserType);
}

class _OptionIdentityState extends State<OptionIdentity> {
  var setLoginType;
  var setUserType;
  _OptionIdentityState(this.setLoginType, this.setUserType);
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(0, 30.h, 0, 100.h),
      child: Column(
        children: [
          Expanded(
            flex: 3,
            child: Container(
              width: double.infinity,
              child: Center(
                child: Text(
                  '请选择您的身份',
                  style: TextStyle(
                    fontSize: 40.sp,
                    color: Color(0xff333333),
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
          ),
          Expanded(
            flex: 3,
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.fromLTRB(0, 0, 0, 10.h),
                  width: double.infinity,
                  child: Center(
                    child: ClipOval(
                      child: InkWell(
                        child: Container(
                          height: 160.h,
                          width: 160.h,
                          color: Color(0xffF8BE45),
                        ),
                        onTap: () {
                          this.setUserType(4);
                          this.setLoginType(false);
                        },
                      ),
                    ),
                  ),
                ),
                Text(
                  "我是家长",
                  style: TextStyle(
                    color: Color(0xff2b2b2b),
                    fontSize: 25.sp,
                  ),
                ),
                Text(
                  "(含学生)",
                  style: TextStyle(
                    color: Color(0xff999999),
                    fontSize: 22.sp,
                  ),
                )
              ],
            ),
          ),
          Expanded(
            flex: 3,
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 10.h),
                  width: double.infinity,
                  child: Center(
                    child: ClipOval(
                      child: InkWell(
                        child: Container(
                          height: 160.h,
                          width: 160.h,
                          color: Color(0xff155ee0),
                        ),
                        onTap: () {
                          this.setUserType(3);
                          this.setLoginType(false);
                        },
                      ),
                    ),
                  ),
                ),
                Text(
                  "我是老师",
                  style: TextStyle(
                    color: Color(0xff2b2b2b),
                    fontSize: 25.sp,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class Login extends StatefulWidget {
  var setLoginType;
  var userType;
  Login(this.setLoginType, this.userType);

  @override
  _LoginState createState() => _LoginState(setLoginType, userType);
}

class _LoginState extends State<Login> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  TextEditingController _username = new TextEditingController();
  TextEditingController _password = new TextEditingController();
  var setLoginType;
  var userType;
  _LoginState(this.setLoginType, this.userType);
  Map form = {};
  @override
  void initState() {
    form['type'] = this.userType;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      physics: BouncingScrollPhysics(),
      child: Container(
        padding: EdgeInsets.fromLTRB(75.w, 100.h, 75.w, 50.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            InkWell(
              child: Icon(
                Icons.clear,
                size: 24,
              ),
              onTap: () {
                this.setLoginType(true);
              },
            ),
            SizedBox(height: 97.h),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '您好,',
                  style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: 40.sp,
                    fontWeight: FontWeight.w600,
                  ),
                  textAlign: TextAlign.start,
                ),
                Text(
                  '欢迎来到如小德',
                  style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: 40.sp,
                    fontWeight: FontWeight.w600,
                  ),
                  textAlign: TextAlign.start,
                ),
                SizedBox(height: 211.h),
                Form(
                  key: _formKey,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '账号',
                        textAlign: TextAlign.start,
                        style: TextStyle(
                          color: Color(0xff333333),
                          fontWeight: FontWeight.w600,
                          fontSize: 32.sp,
                        ),
                      ),
                      TextFormField(
                        controller: _username,
                        decoration: InputDecoration(
                          hintText: '请输用户名',
                        ),
                        validator: (value) {
                          if (value.isEmpty) {
                            return '用户名不能为空';
                          }
                          return null;
                        },
                      ),
                      SizedBox(
                        height: 78.h,
                      ),
                      Text(
                        '密码',
                        textAlign: TextAlign.start,
                        style: TextStyle(
                          color: Color(0xff333333),
                          fontWeight: FontWeight.w600,
                          fontSize: 32.sp,
                        ),
                      ),
                      TextFormField(
                        obscureText: true,
                        controller: _password,
                        decoration: InputDecoration(
                          hintText: '请输入密码',
                        ),
                        validator: (value) {
                          if (value.isEmpty) {
                            return '密码不能为空';
                          }
                          return null;
                        },
                      )
                    ],
                  ),
                )
              ],
            ),
            SizedBox(height: 113.h),
            SizedBox(
              width: 600.w,
              height: 90.h,
              child: RaisedButton(
                color: Color(0xff155EE0),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(22.0)),
                ),
                child: Text(
                  "登录",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 34.sp,
                  ),
                ),
                onPressed: () async {
                  if (_formKey.currentState.validate()) {
                    form['type'] == 3
                        ? form['username'] = _username.text
                        : form['device_no'] = _username.text;
                    form['password'] = _password.text;
                    var http =
                        await Http(context).postHttp('/auth/login', form);
                    if (http['code'] == 200) {
                      Provider.of<GlobalData>(context, listen: false)
                          .setToken(http['data']['access_token']);
                      add('setString', 'token', http['data']['access_token']);
                      var info = await Http(context).postHttp('/auth/info', '');
                      Provider.of<GlobalData>(context, listen: false)
                          .setType(info['data']['type']);
                      add('setInt', 'info_type', info['data']['type']);
                      Provider.of<GlobalData>(context, listen: false)
                          .setSchoolId(info['data']['school_id']);
                      add('setInt', 'school_id', info['data']['school_id']);
                      Navigator.of(context).pushReplacementNamed('/home');
                    }
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
